<template>
	<om-addpage style="padding-top: 35px;" :busCode="busCode" @formSubmit="formSubmit">
		<om-autoform style="" ref="autoform" :initConfigData="mainConfigData" :initValue="initValue"></om-autoform>
	</om-addpage>
</template>

<script>
	import {
		getUrl,
		confirmModal,
		ptFloat,
		minusF,
		errorModal,
		ignoreHtml
	} from '@/static/js/common.js';
	import {
		buildFormData,
		formValidation,
		tranferToEditors,
		changeEditor,
		setSubmitSubValue,
		configSelectData,
		hasValue,
		formShowLoading,
		getFormFields,
		formModelLoad,
		getWorkCacheKey
	} from '@/static/js/formUtil.js';
	import urlUtil from '@/static/js/urlUtil.js';
	import { mapState } from 'pinia';
	import {useLoginUserStore} from '@/stores/login-user.js';
	import moment from '@/static/js/moment.js'
	export default {
		computed: {
			...mapState(useLoginUserStore, ['userName', 'userId'])
		},
		provide() {
			return {
				formPage: this
			}
		},
		data() {
			return buildFormData({
				DICT_ADJUST_TYPE6_3: '',
				dict_level: '',
				leveList: [],
				levelIdCacheKey: "hr-wf-fund-levelId-cache",
				userCacheKey:"hr-wf-fund-user-cache"
			})
		},
		methods: {
			changeLevelType(){
				let _that = this
				_that.$refs.autoform.changeEditor('itemInfo', 'levelId',"type","linkbox")
				_that.$refs.autoform.changeEditor('itemInfo', 'levelId', {
					link:"../../public/pageSearch/pageSearch",
					"linkParam": {
						other: JSON.stringify({
							code: _that.dict_level
						}),
						initload: true,
						namefield: "referNum",
						notefield:"remark",
						extrafield:"dicname",
						url: "tsysdict/findChildrenByCodeNoIsdel",
						cachename: _that.levelIdCacheKey,
					}
				})
			},
			afterUser(id){
				let _that = this
				_that.om.request(
					"thousingFund/afterUser",
					{
						userId:id
					}
				).then(res=>{
					if(res.value.user){
						let formdata = _that.$refs.autoform.formdata
						let d = res.value.user
						formdata.entryDate = d.entryDate
						formdata.sexName = d.sexName
						formdata.idCard = d.idCard
						formdata.salaryPosName = d.salaryPosName
						formdata.instName = d.instName
						formdata.posGrade = d.posGrade
					}
				})
			},
			onInputAmount(val){
				let _that = this
				let result = Number(val) - Number(_that.$refs.autoform.formdata.levelNum)
				if( result > 0){
					_that.$refs.autoform.formdata.overAmount = result * 2
				}else{
					_that.$refs.autoform.clear("itemInfo","overAmount")
				}
			},
			formSubmit() {
				let _that = this
				if(!_that.checkedit){
					_that.om.formSave('thousingFund/save', _that.formdata, _that);
				}else{
					_that.om.formSave('commonForm/doCheckOrApproveUpdate', _that.formdata, _that);
				}
			},
			onSelectadjust(item){
				let _that = this
				if(item.code === "38412"){
					_that.$refs.autoform.clear('itemInfo','levelId')
					_that.$refs.autoform.clear('itemInfo','otherAmounts')
					_that.$refs.autoform.changeEditor('itemInfo','levelId','type',"labelbox")
					_that.$refs.autoform.changeEditor('itemInfo','otherAmounts','type',"labelbox")
					_that.$refs.autoform.clear('itemInfo','overAmount')
				}else{
					_that.changeLevelType()
					_that.$refs.autoform.changeEditor('itemInfo','otherAmounts','type',"numberbox")
				}
			},
			selectZposFunc(e) {
				let _that = this
				let obj = {
					zposId: e.id
				}
				_that.om.request(
					"wxHousingFund/afterZpos",
					obj,
					"get"
				).then(res => {
					if(res.value.user){
						_that.$refs.autoform.formdata.userName = _that.userName
						_that.$refs.autoform.formdata.userId = _that.userId
						_that.$refs.autoform.formdata.entryDate = res.value.user.entryDate
						_that.$refs.autoform.formdata.salaryPosName = res.value.user.salaryPosName
						_that.$refs.autoform.formdata.instName = res.value.user.instName
					}else{
						_that.$refs.autoform.changeEditor("userInfo","userId","type","linkbox")
						_that.$refs.autoform.changeEditor("userInfo","userId",{
							link:"../../public/pageSearch/pageSearch",
							"linkParam":{
								other:JSON.stringify({
									isContainLegal: false,
									isContainLeave: true,
									filterNullSalaryPos: false
								}),
								url:"temployee/getStaffCombogrid",
								initload:true,
								namefield:"username",
								extrafield:'userno',
								notefield:'salaryPosName',
								cachename:_that.userCacheKey,
								searchfield:'q'
							}
						})
					}
				})

				let obj2 = {
					userId: _that.userId
				}
				_that.om.request(
					"wxHousingFund/getAdjustList",
					obj2,
					"get"
				).then(res => {
					_that.$refs.autoform.changeEditor('itemInfo', 'adjustCode', 'data', configSelectData(res, 'code', 'dicname'))
					_that.$refs.autoform.initSelectIndex('itemInfo', 'adjustCode')
				})

			},
			initLoad(options) {
				let _that = this
				let fileId = options.fileId
				let ajaxArr = [
					urlUtil.zposUrl(options.code),
					{
						url:'thousingFund/getEffectMonthList',
						param:{
							ztime:moment().format('YYYY-MM-DD HH:mm')
						}
					}
				]
				if(fileId){
					ajaxArr.push({
						url:"thousingFund/getInfo",
						param:{
							id:fileId
						}
					})
				}
				formModelLoad(ajaxArr, function(res, mainData) {
					changeEditor("itemInfo","effectMonth","data",configSelectData(res[1],'value','name'),mainData)
					if(res[0].length){
						changeEditor('topinfo', 'zposId', 'data', configSelectData(res[0], 'id', 'posName'), mainData)											
					}else{
						_that.om.toast('您在OM系统中没有任职岗位，不能申请公积金',3000)
					}
					if(fileId){
						let fieldArr = getFormFields(mainData)
						fieldArr.push("id")
						for(let index in res[2].value.t){
							if(fieldArr.includes(index)){
								if(index == 'levelNum'){
									if(res[2].value.t[index] == 0){
										_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[2].value.t[index]+'')
										_that.$set(_that.formdata,index,res[2].value.t[index]+'')
									}else{
										_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[2].value.t[index])
										_that.$set(_that.formdata,index,res[2].value.t[index])
									}
								}else{
									_that.$refs.autoform.$set(_that.$refs.autoform.formdata,index,res[2].value.t[index])
									_that.$set(_that.formdata,index,res[2].value.t[index])
								}
							}
						}
						_that.$nextTick(function(){
							_that.changeLevelType()
							if(!_that.checkedit){
								_that.$refs.autoform.initSelectIndex("topinfo","zposId")
								_that.selectZposFunc({id:_that.$refs.autoform.formdata.zposId})
							}
						})
					}
					_that.$nextTick(function(){
						if(!_that.$refs.autoform.formdata.zposId){
							_that.$refs.autoform.initSelectVal("topinfo","zposId")
						}
						_that.$refs.autoform.initSelectIndex("itemInfo","effectMonth")
					})
				}, options)
			}
		},
		onShow() {
			let _that = this;
			if (_that.$refs.autoform) {
				uni.getStorage({
					key: _that.levelIdCacheKey,
					success: function(res) {
						if (res.data) {
							let d = res.data;
							let formdata = _that.$refs.autoform.formdata
							if(_that.formdata.adjustCode != "38035"){
								formdata.levelNum = d.referNum + ''
								formdata.levelId = d.id
								formdata.otherAmounts = d.referNum
							}
							uni.removeStorage({
								key: _that.levelIdCacheKey
							});
						}
					}
				});
				uni.getStorage({
					key:_that.userCacheKey,
					success:(res)=>{
						if(res.data){
							let d = res.data
							let formdata = _that.$refs.autoform.formdata
							formdata.userName = d.username
							formdata.userId = d.id
							_that.afterUser(d.id)
							uni.removeStorage({
								key:_that.userCacheKey
							})
						}
					}
				})
			}
		},
		onLoad(options) {
			let _that = this
			_that.busCode = options.code;
			_that.om.request('wxHousingFund/getConst').then(res => {
				if (res.succeed) {
					_that.DICT_ADJUST_TYPE6_3 = res.value.DICT_ADJUST_TYPE6_3; //退保
					_that.dict_level = res.value.dict_level;
					_that.initLoad(options);
				}
			});
		},

	}
</script>

<style>
</style>
